package quartz_exercise.simpleexercise;

import lombok.SneakyThrows;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class SimpleExerciseMain {
    private SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


//    @PostConstruct
    @SneakyThrows
    public void simpleExercise() {

        SchedulerFactory sf=new StdSchedulerFactory();
        Scheduler scheduler = sf.getScheduler();

        JobDetail jobDetail= JobBuilder.newJob(SimpleJob.class)
                .withDescription("这是简单的测试实例")
                .withIdentity("simpleExercise","group1")
                .build();
        Date nowDate = new Date();
        String nowTime = this.sf.format(nowDate);
        JobDataMap jobDataMap = jobDetail.getJobDataMap();
        jobDataMap.put("name","张三");
        jobDataMap.put("nowTime",nowTime);


        Date beginTime= new Date(nowDate.getTime()+3000);

        Date endTime=new Date(nowDate.getTime()+20000);
        Trigger trigger=TriggerBuilder.newTrigger()
                .withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(2))
                .startAt(beginTime)
                .endAt(endTime)
                .build();

        scheduler.scheduleJob(jobDetail,trigger);

        scheduler.start();

    }

}
